import time
from .data import load_getAlerts,baseurl
import allure
import pytest
import requests
import os

basedir = os.path.dirname(__file__)
filepath = os.path.join(basedir, "testCase/getAlerts.json")

@allure.epic("获取报警记录接口")
class TestGetDeviceEcg:
    def setup_class(self):
        self.session = requests.session()
        self.url_login = f"{baseurl}/login"
        self.url_alerts = f"{baseurl}/alerts"
        print("获取报警记录接口测试开始：", time.strftime("%Y-%m-%d %H:%M:%S"))

    def teardown_class(self):
        self.session.close()
        print("获取报警记录接口测试结束：", time.strftime("%Y-%m-%d %H:%M:%S"))

    @pytest.mark.parametrize(
        "testCase,login_name,login_password,user,msg,status_code",
        load_getAlerts(filepath)
    )
    @allure.title("{testCase}")
    def test_GetDeviceEcg(self, testCase, login_name, login_password, user, msg, status_code):
        # 登录
        login_res = self.session.post(self.url_login, data={"username": login_name, "password": login_password})
        assert login_res.status_code == 200

        # 获取报警记录
        if user:
            res = self.session.get(f"{self.url_alerts}?user={user}")
        else:
            res = self.session.get(self.url_alerts)

        assert res.status_code == status_code
        assert msg in res.text

        # 清理 cookies
        self.session.cookies.clear()
